import { setUserInfo, getTokenKey} from "../../utils/auth";
import { getLocationInfo} from "../../utils/location.js";
var QQMapWX = require('../../qqmap/qqmap-wx-jssdk.min');
var qqmapsdk = new QQMapWX({
    key: 'FBJBZ-2ZLC3-W4U3L-36L33-K5II7-QSBO7' // 必填
})
const app = getApp();
Page({
  data: {
    username: '',
    password: '',
    isLogin: true,
    isPopShow: false,
    isUnmError: false,
    isPwdError: false,
    province : '陕西省',
    city : '西安市',
    stations: [],
    belongs:"选择单位"
  },
  
  onShow(){
    // 设置当前位置信息
    getLocationInfo().then(res=>{
      this.setData({
        province: res.province,
        city: res.city
      })
    }) 
  },
  checkInput(){

  },

  checkUnm(e){
    var reg = /[0-9A-Za-z\u4e00-\u9fa5!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{2,}/;
    if(!e.detail.value.match(reg) ){
      this.setData({
        isUnmError:true
      })
    }else{
      this.setData({
        isUnmError:false 
      })
    }
  },
  checkPwd(e) {
    var reg = /[0-9A-Za-z\u4e00-\u9fa5!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{6,32}/;
    console.log(e)
    if(!e.detail.value.match(reg)){
      this.setData({
        isPwdError:true
      })
    }else{
      this.setData({
        isPwdError:false
      })
    }
  },
  onPopClose(){
    this.setData({ isPopShow: false });
  },

  bindChange(e) {
    this.setData({
      belongs : this.data.stations[e.detail.value[0]]
    })
  },
  
  async openPop(){
    this.setData({
      isPopShow: true
    })
    const res = await app.call({
      path:'/company/search',
      method: 'POST',
      header: {
          'X-WX-SERVICE': 'fire-assistant-s'
        },
      data:{"province":this.data.province,"cityName":this.data.city, "size":120}
    })
    this.setData({
      stations: res.content,
      belongs: res.content[0]
    })
  },

  confirmPop(){
    this.setData({
      isPopShow: false,
    })
  },

  async onLogin(){
    if(this.data.isUnmError || this.data.isPwdError){
      wx.showToast({
        title: '填写项有错误',
        icon: 'error'
      })
    }else{
      const res = await app.call({
        path:'/wechatUser',
        method: 'POST',
        data: {
          username: this.data.username,
          password: this.data.password,
          companyId: this.data.belongs.id,
          source:"wx"
        }
      })
      console.log(res)
      if(res.user){
        setUserInfo(res)
        wx.switchTab({
          url: '../index/index',
        })
      }else {
        if(res.code===40001001){
          wx.showToast({
            title: '用户名重复',
            icon: 'error'
          })
        }else{
          wx.showToast({
            title: '未知错误',
            icon: 'error'
          })
        }
      }

    }
   
  },

  closePop(){
    this.setData({
      isPopShow: false
    })
    
  }
})